void merge(float numbers[], float temp[], int left, int mid, int right, uint index[], uint index_tmp[])
{
  int i, left_end, num_elements, tmp_pos;
 
  left_end = mid - 1;
  tmp_pos = left;
  num_elements = right - left + 1;
 
  while ((left <= left_end) && (mid <= right))
  {
    if (numbers[left] <= numbers[mid])
    {
      temp[tmp_pos] = numbers[left];
      index_tmp[tmp_pos]=index[left];
      tmp_pos = tmp_pos + 1;
      left = left +1;
    }
    else
    {
      temp[tmp_pos] = numbers[mid];
      index_tmp[tmp_pos]=index[mid];
      tmp_pos = tmp_pos + 1;
      mid = mid + 1;
    }
  }
 
  while (left <= left_end)
  {
    temp[tmp_pos] = numbers[left];
    index_tmp[tmp_pos]=index[left];
    left = left + 1;
    tmp_pos = tmp_pos + 1;
  }
  while (mid <= right)
  {
    temp[tmp_pos] = numbers[mid];
    index_tmp[tmp_pos]=index[mid];
    mid = mid + 1;
    tmp_pos = tmp_pos + 1;
  }
 
  for (i=0; i <= num_elements; i++)
  {
    numbers[right] = temp[right];
    index[right]=index_tmp[right];
    right = right - 1;
  }
}

void m_sort(float numbers[], float temp[], int left, int right, uint index[], uint index_tmp[])
{
  int mid;
 
  if (right > left)
  {
    mid = (right + left) / 2;
    m_sort(numbers, temp, left, mid, index, index_tmp);
    m_sort(numbers, temp, mid+1, right, index, index_tmp);
 
    merge(numbers, temp, left, mid+1, right, index, index_tmp);
  }
}
 
uint *mergeSort(float *d,uint n) {
	float temp[n];

	uint *index=new uint[n], index_tmp[n]; 
	for (uint i=0;i<n;i++)
			index[i]=i;

	m_sort(d, temp, 0, n - 1,index,index_tmp);

	return index;	
}
